package com.example.ch05_springboot_web.controller;


import com.example.ch05_springboot_web.config.MessageConfig;
import com.example.ch05_springboot_web.entity.ResponseMessage;
import com.example.ch05_springboot_web.entity.User;
import com.example.ch05_springboot_web.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "控制器：测试Swagger3", description = "描述：测试Swagger3")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;



    // swagger 演示
    @Operation(summary = "测试Swagger3注解方法Get")
    @Parameters({@Parameter(name = "id",description = "编码"),
            @Parameter(name = "headerValue",description = "header传送内容")})
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "400", description = "请求参数没填好"),
            @ApiResponse(responseCode = "401", description = "没有权限"),
            @ApiResponse(responseCode = "403", description = "禁止访问"),
            @ApiResponse(responseCode = "404", description = "请求路径没有或页面跳转路径不对")
    })
    @GetMapping("/testSwagger")   // localhost:8088/user/1     get
    public ResponseMessage get2(@RequestParam @Parameter(example = "2")  Integer id,
                                @RequestHeader @Parameter(example = "2") String headerValue) {
        User userNew = userService.getUserById(id);
        return ResponseMessage.success(userNew);
    }


    // REST
    // 增加
    @PostMapping                // url localhost:8088/user  post
    public ResponseMessage add(@Validated @RequestBody User userDto) {  // userDto 前端可能会封装其它的属性
        User userAdd = userService.addUser(userDto);

        // 前端需要通用的响应数据的格式，而不是随意自定义的文本，所以这里封装一个返回的对象，其它Controller 接口中的方法同理
        return ResponseMessage.success(userAdd);
    }

    // 查询
    @GetMapping("/{userId}")   // localhost:8088/user/1     get
    public ResponseMessage get( @PathVariable Integer userId) {  // userDto 前端可能会封装其它的属性
        User userNew = userService.getUserById(userId);
        return ResponseMessage.success(userNew);
    }


    // 修改
    @PutMapping                 // url localhost:8088/user  put
    public ResponseMessage edt(@Validated @RequestBody User userDto) {  // userDto 前端可能会封装其它的属性
        userService.update(userDto);
        return ResponseMessage.success(null);
    }

    // 删除
    @DeleteMapping("/{userId}")
    public ResponseMessage delete( @PathVariable Integer userId) {  // userDto 前端可能会封装其它的属性
        userService.deleteUser(userId);
        return ResponseMessage.success();
    }




}



